﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.Plumbing
{
	public class AutoDesignHelper
	{
		public List<Pipe> PipeConnectEquipmentBatch(List<FamilyInstance> fis, Pipe pipe, ref string errorMSG)
		{
			List<Pipe> list = new List<Pipe>();
			Document document = pipe.Document;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
			XYZ xyz2 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
			List<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
			bool flag = false;
			foreach (FamilyInstance familyInstance in fis)
			{
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && (int)connector.Domain == 3 && connector.PipeSystemType.ToString() == "Sanitary" && !connector.IsConnected)
					{
						autoConnectParameter.ElementId = familyInstance.Id;
						autoConnectParameter.LocationPoint = (familyInstance.Location as LocationPoint);
						autoConnectParameter.PointOnDuct = hypiping.GetProject(familyInstance);
						autoConnectParameter.DistanceOfEnd = xyz.DistanceTo(autoConnectParameter.PointOnDuct);
						list2.Add(autoConnectParameter);
						flag = true;
					}
				}
			}
			if (!flag)
			{
				return null;
			}
			list2 = (from c in list2
			orderby c.DistanceOfEnd
			select c).ToList<AutoConnectParameter>();
			Pipe pipe2 = pipe;
			new List<Pipe>();
			for (int i = 0; i < list2.Count; i++)
			{
				FamilyInstance fi = document.GetElementById(list2.ElementAt(i).ElementId) as FamilyInstance;
				List<Pipe> list3 = this.PipeConnectEquipment(fi, pipe2, ref errorMSG);
				double num = -1.0;
				foreach (Pipe pipe3 in list3)
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe3);
					double distanceSumEnd = hypiping2.GetDistanceSumEnd(xyz);
					if (num < distanceSumEnd)
					{
						pipe2 = pipe3;
						num = distanceSumEnd;
					}
					if (i == 0)
					{
						if (xyz.DistanceTo(hypiping2.StartConnector.Origin) * 304.8 < 10.0 || xyz.DistanceTo(hypiping2.EndConnector.Origin) * 304.8 < 10.0)
						{
							list.Add(pipe3);
						}
					}
					else if (i == list2.Count - 1 && (xyz2.DistanceTo(hypiping2.StartConnector.Origin) * 304.8 < 10.0 || xyz2.DistanceTo(hypiping2.EndConnector.Origin) * 304.8 < 10.0))
					{
						list.Add(pipe3);
					}
				}
			}
			return list;
		}

		public List<Pipe> PipeConnectEquipment(FamilyInstance fi, Pipe pipe, ref string errorMSG)
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			List<Pipe> list = new List<Pipe>();
			LayTrapsParam layTrapsParam = new LayTrapsParam();
			Document document = fi.Document;
			HYDrainageSystemSetting instance = HYDrainageSystemSetting.Instance;
			FamilySymbol settedFamilySymbol = instance.GetSettedFamilySymbol(document, HYFamilySymbolSign.Tee);
			bool flag = false;
			if (settedFamilySymbol.Family.Name.Contains("顺水"))
			{
				flag = true;
			}
			double num = 0.0;
			Level level = null;
			if (fi != null)
			{
                layTrapsParam.Conn = YJKRevitTools.GetElementOutConnector(fi, (Autodesk.Revit.DB.Domain)3);
				if (layTrapsParam.Conn == null)
				{
					errorMSG = "器具没有排水接口";
					list.Add(pipe);
					return list;
				}
				if (document.ActiveView.GenLevel != null)
				{
					num = document.ActiveView.GenLevel.Elevation;
					level = document.ActiveView.GenLevel;
				}
				else
				{
					num = YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document).Elevation;
					level = YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document);
				}
				if (hvacuserSetting.GetConnectTrapText() == "0")
				{
					layTrapsParam.TrapsType = 2;
					layTrapsParam.Fs = instance.GetSettedFamilySymbol(document, HYFamilySymbolSign.SBend);
				}
				else if (hvacuserSetting.GetConnectTrapText() == "1")
				{
					layTrapsParam.TrapsType = 1;
					layTrapsParam.Fs = instance.GetSettedFamilySymbol(document, HYFamilySymbolSign.PBend);
				}
				double num2 = layTrapsParam.Conn.Origin.Z - num;
				if (num2 < -0.001)
				{
					num2 = 0.0;
				}
				double num3 = num2 + NumbericTools.mmToFeet(instance.RPLength);
				layTrapsParam.SPipeXYZ = layTrapsParam.Conn.Origin + new XYZ(0.0, 0.0, -1.0) * num3;
			}
			HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
			string text = NumbericTools.feetTomm(layTrapsParam.Conn.Radius * 2.0).ToString();
			instance.InitialPipeSystemType(document);
			PipingSystemType pipingSystemType = instance.PipingSystemType;
			PipeType hypipeType = instance.HYPipeType;
			Pipe pipe2 = hycreatePipeHelper.DrawRiserPipe(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, document, hypipeType, text, pipingSystemType);
			YJKRevitTools.ConnectTwoElement(fi, pipe2);
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			if (hvacuserSetting.GetConnectTrapText() != "2")
			{
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				XYZ dlineP = YJKRevitTools.GetDLineP(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ);
				XYZ vector = dlineP - dlineP2;
				double angle = YJKRevitTools.GetTwoXYVectorAngle(XYZ.BasisX, vector) * 180.0 / Math.PI;
				FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(document, layTrapsParam.SPipeXYZ, layTrapsParam.Fs, level, angle);
				YJKRevitTools.SetFamilyInstanceOffSet(familyInstance, level, layTrapsParam.SPipeXYZ.Z - num);
				string a = "";
				YJKRevitTools.SetFamilyInstanceDiameter(familyInstance, double.Parse(text), ref a);
                //a != "";
				document.Regenerate();
				Connector zvectorConnector = this.GetZVectorConnector(familyInstance);
				XYZ origin = zvectorConnector.Origin;
				XYZ connectorDirectionPoint = YJKRevitTools.GetConnectorDirectionPoint(zvectorConnector, layTrapsParam.Conn.Radius * 4.0);
				YJKRevitTools.ConnectTwoElement(familyInstance, pipe2);
				Pipe pipe3 = null;
				if (layTrapsParam.TrapsType == 2)
				{
					pipe3 = hycreatePipeHelper.DrawRiserPipe(zvectorConnector.Origin, connectorDirectionPoint, document, hypipeType, text, pipingSystemType);
					YJKRevitTools.ConnectTwoElement(familyInstance, pipe3);
				}
				document.Regenerate();
				if (layTrapsParam.TrapsType == 1)
				{
					XYZ origin2 = this.GetZVectorConnector(familyInstance).Origin;
					YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					hyline.MakeUnBound();
					XYZ project = hyline.GetProject(origin2);
					bool flag2 = hyline.IsPointOnLine(project);
					double num4 = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
					XYZ xyz = new XYZ(origin2.X, origin2.Y, project.Z);
					double num5 = xyz.DistanceTo(project) * num4;
					XYZ end = new XYZ(origin2.X, origin2.Y, xyz.Z + num5);
					Pipe pipe4 = document.CreatHYPipe(pipingSystemType, hypipeType, level, project, end, double.Parse(text));
					familyInstance.Location.Move(new XYZ(0.0, 0.0, -(origin2.Z - xyz.Z - num5)));
					this.ConnectElement(familyInstance, pipe4);
					document.Regenerate();
					YJKPipeConnectorHelper hypipeConnectorHelper2 = new YJKPipeConnectorHelper();
					List<Pipe> list2 = new List<Pipe>();
					if (flag2)
					{
						list2 = hypiping.Separate(project);
						if (flag)
						{
							hypipeConnectorHelper2.NewTeeFittingY(this.newPipeListUseForTeeFitting(list2, pipe4), settedFamilySymbol, flag);
							list2.Add(pipe4);
						}
						else
						{
							list2.Add(pipe4);
							IList<Pipe> pipes = new List<Pipe>(list2);
							hypipeConnectorHelper2.NewTeeFittingY(pipes, settedFamilySymbol, false);
						}
						list = list2;
					}
					else
					{
						document.Create.NewElbowFitting(this.getCloserConnector(pipe4, project), this.getCloserConnector(pipe, project));
						list2.Add(pipe4);
						list2.Add(pipe);
						list = list2;
					}
					document.Regenerate();
					using (List<Pipe>.Enumerator enumerator = list2.GetEnumerator())
					{
						if (enumerator.MoveNext())
						{
							Pipe pipe5 = enumerator.Current;
							ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, new XYZ(0.0001, 0.0001, 0.0001));
							ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, -1.0 * new XYZ(0.0001, 0.0001, 0.0001));
						}
					}
				}
				if (layTrapsParam.TrapsType == 2)
				{
					list = hypipeConnectorHelper.TPipeConnectRPipe(pipe3, pipe, document, settedFamilySymbol, true, 4, ref errorMSG, false);
				}
				if (errorMSG != "")
				{
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe3);
					XYZ dlineP3 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
					hypiping2.Extend(origin, dlineP3);
				}
			}
			else
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(pipe2, pipe, document, settedFamilySymbol, true, 4, ref errorMSG, false);
			}
			return list;
		}

		public List<Pipe> PipeConnectEquipmentBatch(List<FamilyInstance> fis, Pipe pipe, FamilySymbol fsTee, FamilySymbol fsBend, bool is45Elbow, ref string errorMSG)
		{
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			List<Pipe> list = new List<Pipe>();
			Document document = pipe.Document;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
			XYZ xyz2 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
			List<AutoConnectParameter> list2 = new List<AutoConnectParameter>();
			bool flag = false;
			IList<Element> list3 = new List<Element>();
			foreach (FamilyInstance familyInstance in fis)
			{
				list3.Add(familyInstance);
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				if (familyInstance.MEPModel.ConnectorManager != null)
				{
					ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
						object obj = connectorSetIterator.Current;
						Connector connector = obj as Connector;
						if (connector != null && (int)connector.Domain == 3 && connector.PipeSystemType.ToString() == "Sanitary" && !connector.IsConnected)
						{
							autoConnectParameter.Doc = familyInstance.Document;
							autoConnectParameter.ElementId = familyInstance.Id;
							autoConnectParameter.LocationPoint = (familyInstance.Location as LocationPoint);
							autoConnectParameter.PointOnDuct = hypiping.GetProject(familyInstance);
							autoConnectParameter.DistanceOfEnd = xyz.DistanceTo(autoConnectParameter.PointOnDuct);
							list2.Add(autoConnectParameter);
							flag = true;
							break;
						}
					}
				}
			}
			if (!flag)
			{
				return null;
			}
			list2 = (from c in list2
			orderby c.DistanceOfEnd
			select c).ToList<AutoConnectParameter>();
			List<Pipe> list4 = new List<Pipe>();
			int num = 1;
			IList<AutoConnectParameter> list5 = new List<AutoConnectParameter>();
			ConnectHelper connectHelper = new ConnectHelper();
			IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(pipe, list3);
			IList<AutoConnectParameter> list6 = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(xyz, xyz2, listParam), xyz, xyz2);
			int i = 0;
			while (i < list2.Count)
			{
				int j;
				for (j = i; j < list2.Count; j++)
				{
					if (list6[j].IndexOfDuct != num)
					{
						i = j - 1;
						break;
					}
					list5.Add(list6[j]);
				}
				if (i != j - 1)
				{
					i = j - 1;
				}
				if (list5.Count > 1)
				{
					double num2 = 0.0;
					string text = "";
					double a = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
					IList<Element> list7 = new List<Element>();
					Element elem = null;
					for (int k = 0; k < list5.Count; k++)
					{
						Element elementById = document.GetElementById(list5[k].ElementId);
						list7.Add(elementById);
						LocationPoint locationPoint = elementById.Location as LocationPoint;
						double num3 = YJKRevitTools.DistanceTwoXYZ(hypiping.GetProject(locationPoint.Point), locationPoint.Point);
						if (num3 > num2)
						{
							num2 = num3;
							elem = document.GetElementById(list5[k].ElementId);
						}
					}
					XYZ origin = YJKRevitTools.GetElementOutConnector(elem, (Autodesk.Revit.DB.Domain)3).Origin;
					XYZ lineProject = hypiping.GetLineProject(origin);
					double num4 = new XYZ(origin.X, origin.Y, lineProject.Z).DistanceTo(lineProject);
					double num5 = Math.Tan(a) * num4;
					Pipe pipe2 = null;
					if (fsBend != null && fsBend.Family.Name.Contains("P"))
					{
                        Connector elementInConnector = YJKRevitTools.GetElementInConnector(elem, (Autodesk.Revit.DB.Domain)3);
						XYZ xyz3;
						if (elementInConnector != null)
						{
							xyz3 = origin + elementInConnector.CoordinateSystem.BasisZ * 800.0 / 304.8;
						}
						else
						{
							xyz3 = origin;
						}
						XYZ lineProject2 = hypiping.GetLineProject(xyz3);
						pipe2 = YJKRevitTools.NewPipe(pipe, lineProject2, new XYZ(xyz3.X, xyz3.Y, lineProject2.Z + num5));
						hyrotateFamilyInstance.MovePiping(pipe2);
					}
					else
					{
						XYZ xyz4 = new XYZ(origin.X, origin.Y, lineProject.Z + num5);
						xyz4 += (lineProject - xyz4).Normalize();
						pipe2 = YJKRevitTools.NewPipe(pipe, lineProject, xyz4);
						hyrotateFamilyInstance.MovePiping(pipe2);
					}
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
					XYZ origin2 = hypiping2.StartConnector.Origin;
					XYZ origin3 = hypiping2.EndConnector.Origin;
					listParam = connectHelper.InitYJKElement(pipe2, list7);
					IEnumerable<AutoConnectParameter> enumerable = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(origin2, origin3, listParam), origin2, origin3);
					pipe = this.GetNextPipe(xyz, pipe2, pipe, document, fsTee, ref text);
					document.Regenerate();
					IEnumerator<AutoConnectParameter> enumerator2 = enumerable.GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							AutoConnectParameter autoConnectParameter2 = enumerator2.Current;
							list4 = this.PipeConnectEquipmentList(document.GetElementById(autoConnectParameter2.ElementId) as FamilyInstance, pipe2, fsTee, fsBend, is45Elbow, ref errorMSG);
							if (list4 != null)
							{
								double num6 = -1.0;
								foreach (Pipe pipe3 in list4)
								{
									double distanceSumEnd = new YJKPiping<Pipe>(pipe3).GetDistanceSumEnd(origin2);
									if (num6 < distanceSumEnd)
									{
										pipe2 = pipe3;
										num6 = distanceSumEnd;
									}
								}
							}
							document.Regenerate();
						}
						goto IL_76C;
					}
					goto IL_59F;
				}
				goto IL_59F;
				IL_76C:
				list5.Clear();
				document.Regenerate();
				num++;
				i++;
				continue;
				IL_59F:
				FamilyInstance fi = list2.ElementAt(i).Doc.GetElementById(list2.ElementAt(i).ElementId) as FamilyInstance;
				list4 = this.PipeConnectEquipment(fi, pipe, fsTee, fsBend, is45Elbow, ref errorMSG);
				HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				if (list4 != null)
				{
					double num7 = -1.0;
					foreach (Pipe pipe4 in list4)
					{
						YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe4);
						XYZ basisZ = hypiping3.EndConnector.Connector.CoordinateSystem.BasisZ;
						ElementTransformUtils.MoveElement(document, pipe4.Id, 0.001 * basisZ);
						ElementTransformUtils.MoveElement(document, pipe4.Id, -0.001 * basisZ);
						double distanceSumEnd2 = hypiping3.GetDistanceSumEnd(xyz);
						if (num7 < distanceSumEnd2)
						{
							pipe = pipe4;
							num7 = distanceSumEnd2;
						}
						if (i == 0)
						{
							if (xyz.DistanceTo(hypiping3.StartConnector.Origin) * 304.8 < 10.0 || xyz.DistanceTo(hypiping3.EndConnector.Origin) * 304.8 < 10.0)
							{
								list.Add(pipe4);
							}
						}
						else if (i == list2.Count - 1 && (xyz2.DistanceTo(hypiping3.StartConnector.Origin) * 304.8 < 10.0 || xyz2.DistanceTo(hypiping3.EndConnector.Origin) * 304.8 < 10.0))
						{
							list.Add(pipe4);
						}
					}
					goto IL_76C;
				}
				goto IL_76C;
			}
			return list;
		}

		private Pipe GetNextPipe(XYZ startPoint, Pipe riserPipe, Pipe pipe, Document doc, FamilySymbol fs, ref string message)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			List<Pipe> list = new List<Pipe>();
			list = hypipeConnectorHelper.TPipeConnectRPipe(riserPipe, pipe, doc, fs, true, 4, ref message, false);
			if (list != null)
			{
				if (list.Count == 2)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list.ElementAt(0));
					YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(list.ElementAt(1));
					pipe = ((hypiping.GetDistanceSumEnd(startPoint) > hypiping2.GetDistanceSumEnd(startPoint)) ? list.ElementAt(0) : list.ElementAt(1));
				}
				list.Clear();
			}
			doc.Regenerate();
			return pipe;
		}

		public List<Pipe> PipeConnectEquipment(FamilyInstance fi, Pipe pipe, FamilySymbol fsTee, FamilySymbol fsBend, bool is45Elbow, ref string errorMSG)
		{
			HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
			List<Pipe> list = new List<Pipe>();
			LayTrapsParam layTrapsParam = new LayTrapsParam();
			Document document = pipe.Document;
			HYDrainageSystemSetting instance = HYDrainageSystemSetting.Instance;
			if (fi != null)
			{
				layTrapsParam.Conn = YJKRevitTools.GetElementOutConnector(fi, (Autodesk.Revit.DB.Domain)3);
				if (layTrapsParam.Conn == null)
				{
					errorMSG = hyerrorMessage.AddMessage("器具没有排水接口。");
					list.Add(pipe);
					return list;
				}
				XYZ hyendPoint = ((pipe.Location as LocationCurve).Curve as Line).GetEndPoint(0);
				if (layTrapsParam.Conn.Origin.Z < hyendPoint.Z)
				{
					errorMSG = hyerrorMessage.AddMessage("主管应在器具下方。");
					list.Add(pipe);
					return list;
				}
				if (document.ActiveView != null && document.ActiveView.GenLevel != null)
				{
					double elevation = document.ActiveView.GenLevel.Elevation;
					Level genLevel = document.ActiveView.GenLevel;
				}
				else
				{
					double elevation2 = YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document).Elevation;
					YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document);
				}
				double num = NumbericTools.mmToFeet(150.0);
				layTrapsParam.SPipeXYZ = layTrapsParam.Conn.Origin + new XYZ(0.0, 0.0, -1.0) * num;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(layTrapsParam.SPipeXYZ);
			bool flag = Math.Sqrt((project.X - layTrapsParam.Conn.Origin.X) * (project.X - layTrapsParam.Conn.Origin.X) + (project.Y - layTrapsParam.Conn.Origin.Y) * (project.Y - layTrapsParam.Conn.Origin.Y)) < 4.0 * pipe.Diameter;
			bool flag2 = hyline.IsPointOnLine(project);
			bool flag3 = this.pipeIsConnect(project, pipe);
			if (!flag2 && flag3)
			{
				return null;
			}
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
			string text = NumbericTools.feetTomm(layTrapsParam.Conn.Radius * 2.0).ToString();
			instance.InitialPipeSystemType(document);
			PipingSystemType pipingSystemType = document.GetElementById(pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()) as PipingSystemType;
			Pipe pipe2 = hycreatePipeHelper.DrawRiserPipe(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, document, pipe.PipeType, text, pipingSystemType);
			pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(UnitConvert.CovertToAPI(Convert.ToDouble(text), (Autodesk.Revit.DB.DisplayUnitType)2));
			hyrotateFamilyInstance.MovePiping(pipe2);
			if (fi.Document == document && !YJKRevitTools.ConnectTwoElement(fi, pipe2))
			{
				errorMSG = hyerrorMessage.AddMessage("部分器具的排水口无法连接。");
			}
			if (fsBend != null)
			{
				if (fsBend.Family.Name.Contains("P"))
				{
					layTrapsParam.TrapsType = 1;
				}
				else
				{
					layTrapsParam.TrapsType = 2;
				}
				layTrapsParam.Fs = fsBend;
				XYZ dlineP = YJKRevitTools.GetDLineP(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ);
				XYZ vector = dlineP - dlineP2;
				double angle = YJKRevitTools.GetTwoXYVectorAngle(XYZ.BasisX, vector) * 180.0 / Math.PI;
				FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(document, layTrapsParam.SPipeXYZ, layTrapsParam.Fs, angle);
				string text2 = "";
				YJKRevitTools.SetFamilyInstanceDiameter(familyInstance, double.Parse(text), ref text2);
				document.Regenerate();
				if (layTrapsParam.TrapsType == 2)
				{
					double num2 = this.CheckTwoPipesDistance(pipe2, pipe, "S");
					if (num2 != 0.0)
					{
						new YJKRotateFamilyInstance().RotateElem(document, familyInstance, num2);
					}
				}
				YJKRevitTools.ConnectTwoElement(familyInstance, pipe2);
				document.Regenerate();
				if (layTrapsParam.TrapsType == 1)
				{
					list = this.ConnPBending(document, pipe, familyInstance, fsTee, is45Elbow, flag2, text, ref errorMSG);
				}
				if (layTrapsParam.TrapsType == 2)
				{
					list = this.ConnSBending(document, pipe, familyInstance, fsTee, is45Elbow, flag2, flag, text, ref errorMSG);
				}
			}
			else if (!flag2 && is45Elbow && !flag)
			{
				this.ConnDouble45Elbow(pipe2, pipe, document, ref errorMSG);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				list.Add(pipe);
			}
			else
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(pipe2, pipe, document, fsTee, true, 2, ref errorMSG, false);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				document.Regenerate();
			}
			document.Regenerate();
			return list;
		}

		public List<Pipe> PipeConnectLinkEquipment(Document docCur, FamilyInstance fi, Pipe pipe, FamilySymbol fsTee, FamilySymbol fsBend, bool is45Elbow, ref string errorMSG)
		{
			HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
			List<Pipe> list = new List<Pipe>();
			LayTrapsParam layTrapsParam = new LayTrapsParam();
			XYZ xyz = XYZ.Zero;
			Document document = fi.Document;
			HYDrainageSystemSetting instance = HYDrainageSystemSetting.Instance;
			double num = 0.0;
			Level level = null;
			if (fi != null)
			{
				layTrapsParam.Conn = YJKRevitTools.GetElementOutConnector(fi, (Autodesk.Revit.DB.Domain)3);
				if (layTrapsParam.Conn == null)
				{
					errorMSG = hyerrorMessage.AddMessage("器具没有排水接口。");
					list.Add(pipe);
					return list;
				}
				xyz = layTrapsParam.Conn.Origin;
				if (fi.Document.IsLinked)
				{
					xyz = docCur.GetLinkedDocument(fi.Id).GetPointInLinkDocument(xyz);
				}
				XYZ hyendPoint = ((pipe.Location as LocationCurve).Curve as Line).GetEndPoint(0);
				if (xyz.Z < hyendPoint.Z)
				{
					errorMSG = hyerrorMessage.AddMessage("主管应在器具下方。");
					list.Add(pipe);
					return list;
				}
				if (docCur.ActiveView.GenLevel != null)
				{
					num = docCur.ActiveView.GenLevel.Elevation;
					level = docCur.ActiveView.GenLevel;
				}
				else
				{
					num = YJKRevitTools.GetLevelClose(xyz, docCur).Elevation;
					level = YJKRevitTools.GetLevelClose(xyz, docCur);
				}
				double num2 = NumbericTools.mmToFeet(150.0);
				layTrapsParam.SPipeXYZ = xyz + new XYZ(0.0, 0.0, -1.0) * num2;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(layTrapsParam.SPipeXYZ);
			bool flag = Math.Sqrt((project.X - xyz.X) * (project.X - xyz.X) + (project.Y - xyz.Y) * (project.Y - xyz.Y)) < 4.0 * pipe.Diameter;
			bool flag2 = hyline.IsPointOnLine(project);
			bool flag3 = this.pipeIsConnect(project, pipe);
			if (!flag2 && flag3)
			{
				return null;
			}
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
			string text = NumbericTools.feetTomm(layTrapsParam.Conn.Radius * 2.0).ToString();
			instance.InitialPipeSystemType(docCur);
			PipingSystemType pipingSystemType = docCur.GetElementById(pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()) as PipingSystemType;
			Pipe pipe2 = hycreatePipeHelper.DrawRiserPipe(xyz, layTrapsParam.SPipeXYZ, docCur, pipe.PipeType, text, pipingSystemType);
			pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(UnitConvert.CovertToAPI(Convert.ToDouble(text), (Autodesk.Revit.DB.DisplayUnitType)2));
			if (!document.IsLinked && !YJKRevitTools.ConnectTwoElement(fi, pipe2))
			{
				errorMSG = hyerrorMessage.AddMessage("部分器具的排水口无法连接。");
			}
			if (fsBend != null)
			{
				if (fsBend.Family.Name.Contains("P"))
				{
					layTrapsParam.TrapsType = 1;
				}
				else
				{
					layTrapsParam.TrapsType = 2;
				}
				layTrapsParam.Fs = fsBend;
				XYZ dlineP = YJKRevitTools.GetDLineP(xyz, layTrapsParam.SPipeXYZ, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, xyz, layTrapsParam.SPipeXYZ);
				XYZ vector = dlineP - dlineP2;
				double angle = YJKRevitTools.GetTwoXYVectorAngle(XYZ.BasisX, vector) * 180.0 / Math.PI;
				FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(docCur, layTrapsParam.SPipeXYZ, layTrapsParam.Fs, level, angle);
				YJKRevitTools.SetFamilyInstanceOffSet(familyInstance, level, layTrapsParam.SPipeXYZ.Z - num);
				string text2 = "";
				YJKRevitTools.SetFamilyInstanceDiameter(familyInstance, double.Parse(text), ref text2);
				docCur.Regenerate();
				if (layTrapsParam.TrapsType == 2)
				{
					double num3 = this.CheckTwoPipesDistance(pipe2, pipe, "S");
					if (num3 != 0.0)
					{
						new YJKRotateFamilyInstance().RotateElem(docCur, familyInstance, num3);
					}
				}
				this.MoveSOrPBend(familyInstance, layTrapsParam.SPipeXYZ);
				YJKRevitTools.ConnectTwoElement(familyInstance, pipe2);
				docCur.Regenerate();
				if (layTrapsParam.TrapsType == 1)
				{
					list = this.ConnPBending(docCur, pipe, familyInstance, fsTee, is45Elbow, flag2, text, ref errorMSG);
				}
				if (layTrapsParam.TrapsType == 2)
				{
					list = this.ConnSBending(docCur, pipe, familyInstance, fsTee, is45Elbow, flag2, flag, text, ref errorMSG);
				}
			}
			else if (!flag2 && is45Elbow && !flag)
			{
				this.ConnDouble45Elbow(pipe2, pipe, docCur, ref errorMSG);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				list.Add(pipe);
			}
			else
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(pipe2, pipe, docCur, fsTee, true, 2, ref errorMSG, false);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				docCur.Regenerate();
			}
			docCur.Regenerate();
			return list;
		}

		private void MoveSOrPBend(FamilyInstance fiBend, XYZ layPoint)
		{
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(fiBend);
			Connector connector = null;
			XYZ xyz = new XYZ(0.0, 0.0, 1.0);
			foreach (Connector connector2 in elementAllConnector)
			{
                if ((int)connector2.Domain == 3 && connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz, 0.0001))
				{
					connector = connector2;
					break;
				}
			}
			if (connector == null)
			{
				return;
			}
			XYZ moveVector = layPoint.Subtract(connector.Origin);
			new YJKRotateFamilyInstance().MoveElement(moveVector, fiBend);
		}

		public List<Pipe> PipeConnectEquipmentList(FamilyInstance fi, Pipe pipe, FamilySymbol fsTee, FamilySymbol fsBend, bool is45Elbow, ref string errorMSG)
		{
			HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
			List<Pipe> list = new List<Pipe>();
			LayTrapsParam layTrapsParam = new LayTrapsParam();
			Document document = fi.Document;
			HYDrainageSystemSetting instance = HYDrainageSystemSetting.Instance;
			double num = 0.0;
			Level level = null;
			if (fi != null)
			{
				layTrapsParam.Conn = YJKRevitTools.GetElementOutConnector(fi, (Autodesk.Revit.DB.Domain)3);
				if (layTrapsParam.Conn == null)
				{
					errorMSG = hyerrorMessage.AddMessage("器具没有排水接口。");
					list.Add(pipe);
					return list;
				}
				XYZ hyendPoint = ((pipe.Location as LocationCurve).Curve as Line).GetEndPoint(0);
				if (layTrapsParam.Conn.Origin.Z < hyendPoint.Z)
				{
					errorMSG = hyerrorMessage.AddMessage("主管应在器具下方。");
					list.Add(pipe);
					return list;
				}
				if (document.ActiveView.GenLevel != null)
				{
					num = document.ActiveView.GenLevel.Elevation;
					level = document.ActiveView.GenLevel;
				}
				else
				{
					num = YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document).Elevation;
					level = YJKRevitTools.GetLevelClose(layTrapsParam.Conn.Origin, document);
				}
				double num2 = NumbericTools.mmToFeet(150.0);
				layTrapsParam.SPipeXYZ = layTrapsParam.Conn.Origin + new XYZ(0.0, 0.0, -1.0) * num2;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(layTrapsParam.SPipeXYZ);
			bool flag = Math.Sqrt((project.X - layTrapsParam.Conn.Origin.X) * (project.X - layTrapsParam.Conn.Origin.X) + (project.Y - layTrapsParam.Conn.Origin.Y) * (project.Y - layTrapsParam.Conn.Origin.Y)) < 4.0 * pipe.Diameter;
			bool flag2 = hyline.IsPointOnLine(project);
			bool flag3 = this.pipeIsConnect(project, pipe);
			if (!flag2 && flag3)
			{
				return null;
			}
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
			string text = NumbericTools.feetTomm(layTrapsParam.Conn.Radius * 2.0).ToString();
			instance.InitialPipeSystemType(document);
			PipingSystemType pipingSystemType = document.GetElementById(pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()) as PipingSystemType;
			Pipe pipe2 = hycreatePipeHelper.DrawRiserPipe(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, document, pipe.PipeType, text, pipingSystemType);
			pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(UnitConvert.CovertToAPI(Convert.ToDouble(text), (Autodesk.Revit.DB.DisplayUnitType)2));
			if (!YJKRevitTools.ConnectTwoElement(fi, pipe2))
			{
				errorMSG = hyerrorMessage.AddMessage("部分器具的排水口无法连接。");
			}
			if (fsBend != null)
			{
				if (fsBend.Family.Name.Contains("P"))
				{
					layTrapsParam.TrapsType = 1;
				}
				else
				{
					layTrapsParam.TrapsType = 2;
				}
				layTrapsParam.Fs = fsBend;
				XYZ dlineP = YJKRevitTools.GetDLineP(layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, layTrapsParam.Conn.Origin, layTrapsParam.SPipeXYZ);
				XYZ vector = dlineP - dlineP2;
				double angle = YJKRevitTools.GetTwoXYVectorAngle(XYZ.BasisX, vector) * 180.0 / Math.PI;
				if (layTrapsParam.TrapsType == 2)
				{
					angle = 0.0;
				}
				FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(document, layTrapsParam.SPipeXYZ, layTrapsParam.Fs, level, angle);
				YJKRevitTools.SetFamilyInstanceOffSet(familyInstance, level, layTrapsParam.SPipeXYZ.Z - num);
				string text2 = "";
				YJKRevitTools.SetFamilyInstanceDiameter(familyInstance, double.Parse(text), ref text2);
				document.Regenerate();
				if (layTrapsParam.TrapsType == 2)
				{
					XYZ xyz = hypiping.StartConnector.Origin - new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
					double num3 = new XYZ(1.0, 0.0, 0.0).AngleTo(xyz) * 180.0 / Math.PI;
					YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
					if (num3 != 0.0)
					{
						hyrotateFamilyInstance.RotateElem(document, familyInstance, num3);
					}
					Connector zvectorConnector = this.GetZVectorConnector(familyInstance);
					XYZ connectorDirectionPoint = YJKRevitTools.GetConnectorDirectionPoint(zvectorConnector, 0.01);
					Pipe pipe3 = hycreatePipeHelper.DrawRiserPipe(zvectorConnector.Origin, connectorDirectionPoint, document, pipe.PipeType, text, pipingSystemType);
					bool flag4 = hypiping.IntersectionPoint(pipe3) != null;
					document.DeleteElement(pipe3);
					if (!flag4)
					{
						hyrotateFamilyInstance.RotateElem(document, familyInstance, 180.0 - 2.0 * num3);
					}
				}
				YJKRevitTools.ConnectTwoElement(familyInstance, pipe2);
				document.Regenerate();
				if (layTrapsParam.TrapsType == 1)
				{
					list = this.ConnPBending(document, pipe, familyInstance, fsTee, is45Elbow, flag2, text, ref errorMSG);
				}
				if (layTrapsParam.TrapsType == 2)
				{
					list = this.ConnSBending(document, pipe, familyInstance, fsTee, is45Elbow, flag2, flag, text, ref errorMSG);
				}
			}
			else if (!flag2 && is45Elbow && !flag)
			{
				this.ConnDouble45Elbow(pipe2, pipe, document, ref errorMSG);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				list.Add(pipe);
			}
			else
			{
				list = hypipeConnectorHelper.TPipeConnectRPipe(pipe2, pipe, document, fsTee, true, 2, ref errorMSG, false);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				document.Regenerate();
			}
			document.Regenerate();
			return list;
		}

		private List<Pipe> ConnPBending(Document doc, Pipe pipe, FamilyInstance trapsFi, FamilySymbol fsTee, bool is45Elbow, bool isOnLine, string diameter, ref string errorMSG)
		{
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			List<Pipe> result = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			bool flag = fsTee.Family.Name.Contains("顺水");
			XYZ origin = this.GetZVectorConnector(trapsFi).Origin;
			XYZ project = hyline.GetProject(origin);
			double num = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			XYZ xyz = new XYZ(origin.X, origin.Y, project.Z);
			double num2 = xyz.DistanceTo(project) * num;
			XYZ xyz2 = new XYZ(origin.X, origin.Y, xyz.Z + num2);
			trapsFi.Location.Move(new XYZ(0.0, 0.0, -(origin.Z - xyz.Z - num2)));
			if (xyz2.DistanceTo(project) > pipe.Diameter)
			{
				Pipe pipe2 = YJKRevitTools.NewPipe(pipe, project, xyz2);
				hyrotateFamilyInstance.MovePiping(pipe2);
				new YJKPiping<Pipe>(pipe2).SetDiameter(double.Parse(diameter));
				this.ConnectElement(trapsFi, pipe2);
				doc.Regenerate();
				YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
				List<Pipe> list = new List<Pipe>();
				bool flag2 = true;
				Line curve = (pipe.Location as LocationCurve).Curve as Line;
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				double num3 = (project.DistanceTo(hyendPoint) - project.DistanceTo(hyendPoint2) > 0.0) ? project.DistanceTo(hyendPoint2) : project.DistanceTo(hyendPoint);
				double num4 = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
				if (num3 < num4)
				{
					flag2 = false;
				}
				if (isOnLine && flag2)
				{
					list = hypiping.Separate(project);
					if (flag)
					{
						hypipeConnectorHelper.NewTeeFittingY(this.newPipeListUseForTeeFitting(list, pipe2), fsTee, flag);
					}
					else
					{
						IList<Pipe> pipes = new List<Pipe>(list);
						hypipeConnectorHelper.NewTeeFittingY(pipes, fsTee, false);
					}
					result = list;
				}
				else
				{
					if (is45Elbow)
					{
						Line curve2 = (pipe.Location as LocationCurve).Curve as Line;
						Line curve3 = (pipe2.Location as LocationCurve).Curve as Line;
						XYZ xyz3 = (curve2.GetEndPoint(0).DistanceTo(project) > curve2.GetEndPoint(1).DistanceTo(project)) ? curve2.GetEndPoint(0) : curve2.GetEndPoint(1);
						XYZ xyz4 = (curve3.GetEndPoint(0).DistanceTo(project) > curve3.GetEndPoint(1).DistanceTo(project)) ? curve3.GetEndPoint(0) : curve3.GetEndPoint(1);
						hypipeConnectorHelper.connect45Elbow(pipe, xyz3, pipe2, xyz4, doc);
					}
					else
					{
						doc.Create.NewElbowFitting(this.getCloserConnector(pipe2, project), this.getCloserConnector(pipe, project));
					}
					list.Add(pipe);
					result = list;
				}
				doc.Regenerate();
				using (List<Pipe>.Enumerator enumerator = list.GetEnumerator())
				{
					if (!enumerator.MoveNext())
					{
						return result;
					}
					Pipe pipe3 = enumerator.Current;
					ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, new XYZ(0.0001, 0.0001, 0.0001));
					ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, -1.0 * new XYZ(0.0001, 0.0001, 0.0001));
					return result;
				}
			}
			HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
			errorMSG = hyerrorMessage.AddMessage("P弯与管道间距离过小，无法连接。");
			return result;
		}

		private List<Pipe> ConnSBending(Document doc, Pipe pipe, FamilyInstance trapsFi, FamilySymbol fsTee, bool is45Elbow, bool isOnLine, bool isElbow, string diameter, ref string errorMSG)
		{
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			List<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			HYErrorMessage hyerrorMessage = new HYErrorMessage(errorMSG);
			XYZ origin = this.GetZVectorConnector(trapsFi).Origin;
			XYZ project = hyline.GetProject(origin);
			Connector zvectorConnector = this.GetZVectorConnector(trapsFi);
			XYZ origin2 = zvectorConnector.Origin;
			XYZ connectorDirectionPoint = YJKRevitTools.GetConnectorDirectionPoint(zvectorConnector, 0.01);
			HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
			PipingSystemType pipingSystemType = doc.GetElementById(pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()) as PipingSystemType;
			Pipe pipe2 = hycreatePipeHelper.DrawRiserPipe(zvectorConnector.Origin, connectorDirectionPoint, doc, pipe.PipeType, diameter, pipingSystemType);
			pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(UnitConvert.CovertToAPI(Convert.ToDouble(diameter), (Autodesk.Revit.DB.DisplayUnitType)2));
			hyrotateFamilyInstance.MovePiping(pipe2);
			YJKRevitTools.ConnectTwoElement(trapsFi, pipe2);
			if (!isOnLine && is45Elbow && !isElbow)
			{
				this.ConnDouble45Elbow(pipe2, pipe, doc, ref errorMSG);
				errorMSG = hyerrorMessage.AddMessage(errorMSG);
				list.Add(pipe);
			}
			else
			{
				if (zvectorConnector.Origin.Z > project.Z)
				{
					list = new YJKPipeConnectorHelper().TPipeConnectRPipe(pipe2, pipe, doc, fsTee, true, 2, ref errorMSG, false);
					errorMSG = hyerrorMessage.AddMessage(errorMSG);
				}
				else
				{
					doc.DeleteElement(pipe2);
					errorMSG = hyerrorMessage.AddMessage("位置不合适，部分器具无法连接。");
				}
				doc.Regenerate();
			}
			return list;
		}

		private bool pipeIsConnect(XYZ pipeXyz, Pipe pipe)
		{
			bool result = false;
			Line curve = (pipe.Location as LocationCurve).Curve as Line;
			XYZ xyz = (curve.GetEndPoint(0).DistanceTo(pipeXyz) < curve.GetEndPoint(1).DistanceTo(pipeXyz)) ? curve.GetEndPoint(0) : curve.GetEndPoint(1);
			ConnectorSetIterator connectorSetIterator = pipe.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector.Origin.IsAlmostEqualTo(xyz))
				{
					result = connector.IsConnected;
				}
			}
			return result;
		}

		private void ConnDouble45Elbow(Pipe bSPipe, Pipe pipe, Document doc, ref string errorMSG)
		{
			try
			{
				Line curve = (bSPipe.Location as LocationCurve).Curve as Line;
				Line curve2 = (pipe.Location as LocationCurve).Curve as Line;
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				XYZ hyendPoint3 = curve2.GetEndPoint(0);
				XYZ hyendPoint4 = curve2.GetEndPoint(1);
				double num = pipe.GetParameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
				XYZ dlineP = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
				Pipe pipe2 = YJKRevitTools.NewPipe(bSPipe, dlineP, dlineP2);
				doc.Regenerate();
				XYZ hyendPoint5 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0);
				XYZ hyendPoint6 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(1);
				LocationCurve locationCurve = pipe2.Location as LocationCurve;
				double num2 = hyendPoint5.DistanceTo(hyendPoint6);
				if (num != 0.0 && !YJKRevitTools.isRiserPipe(pipe2))
				{
					double num3 = num2 * Math.Sin(num);
					if (hyendPoint5.DistanceTo(dlineP) < hyendPoint6.DistanceTo(dlineP))
					{
						Line curve3 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint5.X, hyendPoint5.Y, hyendPoint5.Z + num3), hyendPoint6);
						locationCurve.Curve = curve3;
					}
					else
					{
						Line curve4 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint6.X, hyendPoint6.Y, hyendPoint6.Z + num3), hyendPoint5);
						locationCurve.Curve = curve4;
					}
				}
				doc.Create.NewElbowFitting(this.getCloserConnector(pipe2, dlineP), this.getCloserConnector(bSPipe, dlineP));
				doc.Regenerate();
				YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
				Line curve5 = (pipe.Location as LocationCurve).Curve as Line;
				Line curve6 = (pipe2.Location as LocationCurve).Curve as Line;
				XYZ xyz = (curve5.GetEndPoint(0).DistanceTo(dlineP2) > curve5.GetEndPoint(1).DistanceTo(dlineP2)) ? curve5.GetEndPoint(0) : curve5.GetEndPoint(1);
				XYZ xyz2 = (curve6.GetEndPoint(0).DistanceTo(dlineP2) > curve6.GetEndPoint(1).DistanceTo(dlineP2)) ? curve6.GetEndPoint(0) : curve6.GetEndPoint(1);
				hypipeConnectorHelper.connect45Elbow(pipe, xyz, pipe2, xyz2, doc);
				doc.Regenerate();
			}
			catch (Exception)
			{
				errorMSG = "位置不合适，部分器具无法连接。";
			}
		}

		private Connector getCloserConnector(Pipe pipe, XYZ xyz)
		{
			return new YJKPiping<Pipe>(pipe).GetCloseConnector(xyz, false);
		}

		private List<Pipe> newPipeListUseForTeeFitting(List<Pipe> listfirst, Pipe pipe)
		{
			List<Pipe> list = new List<Pipe>();
			list.AddRange(YJKRevitTools.judgeWhitchPipeIsHigh(listfirst));
			list.Add(pipe);
			return list;
		}

		private bool ConnectElement(Element elem1, Pipe elem2)
		{
			bool flag = false;
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(elem1);
			List<Connector> connectorAll = YJKRevitTools.GetConnectorAll(elem2);
			foreach (Connector connector in elementAllConnector)
			{
				foreach (Connector connector2 in connectorAll)
				{
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin) && !connector.IsConnected && !connector2.IsConnected)
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
			return flag;
		}

		private Connector GetZVectorConnector(FamilyInstance fi)
		{
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(fi);
			Connector result = null;
			foreach (Connector connector in elementAllConnector)
			{
				if (!connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)))
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private double CheckTwoPipesDistance(Pipe risePipe, Pipe standardPipe, string familySymbol)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(standardPipe);
			double dppipeDistance = hypiping.GetDPPipeDistance(risePipe);
			double num = risePipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			double num2 = standardPipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			double num3 = num;
			if (num2 > num)
			{
				num3 = num2;
			}
			double num4 = 0.0;
			if (familySymbol == "S")
			{
				num4 = 4.0;
			}
			if (familySymbol == "P")
			{
				num4 = 6.0;
			}
			double num5 = num * num4;
			double result = 0.0;
			XYZ elementCurve = YJKRevitTools.GetElementCurve(risePipe, 0);
			List<XYZ> slopPipeEndPoint = hypiping.GetSlopPipeEndPoint();
			if (dppipeDistance > (num4 + 3.0) * num3)
			{
				result = 0.0;
			}
			else if (dppipeDistance <= (num4 + 3.0) * num3 && dppipeDistance > (num4 + 2.0) * num3)
			{
				result = this.GetFIRotateAngle(elementCurve, slopPipeEndPoint[0], slopPipeEndPoint[1], num5, 3);
			}
			else if (dppipeDistance <= (num4 + 2.0) * num3 && dppipeDistance - num5 > 0.01)
			{
				result = this.GetFIRotateAngle(elementCurve, slopPipeEndPoint[0], slopPipeEndPoint[1], num5, 2);
			}
			else if (Math.Abs(dppipeDistance - num5) <= 0.01)
			{
				result = 0.0;
			}
			else if (dppipeDistance - num5 < 0.01)
			{
				result = this.GetFIRotateAngle(elementCurve, slopPipeEndPoint[0], slopPipeEndPoint[1], num5, 0);
			}
			return result;
		}

		private double GetFIRotateAngle(XYZ layPoint, XYZ pipeLowPnt, XYZ pipeHighPnt, double standardDistance, int styleNum)
		{
			double num = 0.0;
			layPoint = new XYZ(layPoint.X, layPoint.Y, 0.0);
			pipeHighPnt = new XYZ(pipeHighPnt.X, pipeHighPnt.Y, 0.0);
			pipeLowPnt = new XYZ(pipeLowPnt.X, pipeLowPnt.Y, 0.0);
			XYZ xyz = pipeLowPnt.Subtract(pipeHighPnt).Normalize();
			YJKLine hyline = new YJKLine(pipeLowPnt, pipeHighPnt);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(layPoint);
			double num2 = project.DistanceTo(layPoint);
			XYZ vector = project.Subtract(layPoint);
			double num3;
			switch (styleNum)
			{
			case 0:
				num3 = Math.Sqrt(standardDistance * standardDistance - num2 * num2);
				goto IL_EA;
			case 2:
				num3 = num2;
				goto IL_EA;
			case 3:
				num3 = Math.Sqrt(3.0) * num2;
				goto IL_EA;
			}
			num3 = -1.0;
			IL_EA:
			XYZ xyz2 = null;
			if (num3 != -1.0)
			{
				xyz2 = project.Add(xyz.Multiply(num3));
			}
			if (num3 == num2)
			{
				xyz2 = layPoint.Subtract(pipeLowPnt);
			}
			XYZ xyz3 = null;
			if (xyz2 != null)
			{
				xyz3 = xyz2.Subtract(layPoint);
			}
			if (xyz3 != null)
			{
				num = YJKRevitTools.GetTwoXYVectorAngle(vector, xyz3);
			}
			return num * 180.0 / Math.PI;
		}
	}
}
